<?php

namespace Superadmin\Model;

use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\Adapter\Adapter;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Sql\Sql; //join
use Superadmin\Model\Categories;
use Zend\Db\Sql\Select;

class CategoriesTable extends AbstractTableGateway {

    protected $table = "tblcategories";
    public $sql;

    public function __construct(Adapter $adapter) {
        $this->adapter = $adapter;
        $this->resultSetPrototype = new ResultSet();
        $this->resultSetPrototype->setArrayObjectPrototype(new Categories());
        $this->initialize();
        $this->sql = new Sql($this->adapter);
    }

    public function showcate($key) {

        $select = $this->sql->select();
        $select->from($this->table);
        if ($key != null) {
            $select->where(array('cateName' => $key));
        }
        $stament = $this->sql->prepareStatementForSqlObject($select);
        $results = $stament->execute();

        $array = array();
        foreach ($results as $result) {
            $tmp = array();
            $tmp = $result;
            $array[] = $tmp;
        }

        return $array;
    }

    public function deltecate($cateId) {
        $sqlex = $this->sql->delete();
        $sqlex->from($this->table);
        $sqlex->where(array('cateId' => $cateId));
        $stament = $this->sql->prepareStatementForSqlObject($sqlex);
        $results = $stament->execute();
        if ($results != null) {
            return true;
        } else {
            return false;
        }
    }

    public function getdataeditcate($cateId) {
        $sqlex = $this->sql->select();
        $sqlex->from($this->table);
        $sqlex->where(array('cateId' => $cateId));
        $stament = $this->sql->prepareStatementForSqlObject($sqlex);
        $results = $stament->execute();
        $data = $results->current();
        return $data;
    }

    public function updatecate(Categories $cate) {

        $data = array(
            'cateName' => $cate->cateName,
            'cateseo' => $cate->cateseo,
            'cateDes' => $cate->cateDes,
            'dateupdate' => $cate->dateupdate,
        );
        $cateId = (int) $cate->cateId;
        $sqlex = $this->sql->update($this->table);
        $sqlex->set($data);
        $sqlex->where(array('cateId' => $cateId));
        $stament = $this->sql->prepareStatementForSqlObject($sqlex);
        $results = $stament->execute();
        if ($results != null) {
            return true;
        } else {
            return false;
        }
    }

    public function addcate(Categories $cate) {
        $data = array(
            'cateName' => $cate->cateName,
            'cateseo' => $cate->cateseo,
            'cateDes' => $cate->cateDes,
            'datecreat' => $cate->datecreat,
            'dateupdate' => $cate->dateupdate,
        );
        $sqlex = $this->sql->insert();
        $sqlex->into($this->table);
        $sqlex->values($data);
        $stament = $this->sql->prepareStatementForSqlObject($sqlex);
        $results = $stament->execute();
        if ($results != null) {
            return true;
        } else {
            return false;
        }
    }

}
